<template>
  <uni-nav-bar title="消息" left-icon="left" @clickLeft="navBack" :fixed="true" statusBar></uni-nav-bar>
  <!-- 头像显示圆点 -->
  <scroll-view scroll-y="true" enable-back-to-top @scrolltolower="loadMore" class="scroll-box animation-slide-right"
    :style="{ height: pageHeight + 'px' }">
    <view v-for="(item,index) in orderLists" :key="index">
      <uni-list-chat :avatar-circle="true" :title="item.title" :avatar="item.image" :note="item.type" :clickable="true"
        :time="item.createtime" badge-positon="left" :badge-text="item.is_see===0?'dot':''"
        @click="jump(item.id)"></uni-list-chat>
    </view>
  </scroll-view>
</template>

<script setup lang="ts">
  import { navTo, navBack } from '@/utils/navigator';
  import { ref } from 'vue';
  import { getMessage } from '@/gql/earthPushing/user';
  import { toPublish } from '@mqtt';
  import { getDriverID } from '@/stores/driverID';
  const staff_id = getDriverID();

  const orderLists = ref([]);
  const isLoading = ref(false);
  const lastPage = ref(1);
  const currentPage = ref(1);

  const pageHeight = ref();
  uni.getSystemInfo({
    success: function (res) {
      pageHeight.value = res.windowHeight - 50;
    },
  });
  init()
  function init() {
    isLoading.value = true;
    const payload = {
      query: getMessage,
      variables: {
        page: currentPage.value,
        staff_id,
      },
    };
    toPublish(
      'ql/staff/getMessage',
      payload,
      (obj : any) => {
        const { getMessage } = obj.data;
        isLoading.value = false;
        orderLists.value = [...orderLists.value, ...getMessage.list];
        lastPage.value = getMessage.lastpage;
      },
    );
  }
  /**
   * 加载更多
   */
  function loadMore() {
    if (currentPage.value < lastPage.value) {
      currentPage.value++;
      init();
    }
  }
  function jump(num : string) {
    orderLists.value = orderLists.value.filter((item) => item.id == num ? item.is_see = 1 : item)
    navTo('Sale/Information/richText?type=Message&id=' + num)
  }
</script>

<style scoped lang="less">
</style>